package com.xungou.shop.controller;

import com.xungou.resource.dao.IResource;
import com.xungou.resource.service.IResourceService;
import com.xungou.shop.pojo.Shop;
import com.xungou.shop.service.IShopService;
import com.xungou.utils.FileUtil;
import com.xungou.utils.SecurityUtil;
import com.xungou.utils.UserUtil;
import org.apache.commons.io.FileUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.xml.registry.infomodel.User;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/shop")
public class ShopController {
    @Resource
    private IShopService shopService;
    @Resource
    private IResourceService iResourceService;

    @RequestMapping("/add")
    @ResponseBody
    public Map<String, Object> add(Shop shop, @RequestParam Map<String, String> params) {
        String status = params.get("status");
        if (status != null && !status.equals("")) {
            Shop oldShop = shopService.getShopByUserId(UserUtil.getCurrentUser().getId());
            shopService.deleteByPrimaryKey(oldShop.getId());
            iResourceService.deleteByPrimaryKey(oldShop.getBusinessLicensePhoto());
            iResourceService.deleteByPrimaryKey(oldShop.getShopInPhoto());
            iResourceService.deleteByPrimaryKey(oldShop.getShopOutPhoto());
        }
        shop.setStatus(Shop.STATUS_CHECK_ING);
        shop.setUserId(UserUtil.getCurrentUser().getId());
        int flag = shopService.insertSelective(shop);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("flag", flag);
        result.put("shopId", shop.getId());
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/uploadPhoto")
    public Map<String, Object> upload(@RequestParam(value = "file") MultipartFile file, @RequestParam Map<String, String> params) {
        com.xungou.resource.pojo.Resource resource = FileUtil.uploadFile(file, "shop", iResourceService);
        Shop shop = new Shop();
        shop.setId(Integer.parseInt(params.get("shopId")));
        String photoName = params.get("photoName");
        if ("businessLicensePhoto".equals(photoName))
            shop.setBusinessLicensePhoto(resource.getId());
        else if ("shopOutPhoto".equals(photoName))
            shop.setShopOutPhoto(resource.getId());
        else if ("shopInPhoto".equals(photoName))
            shop.setShopInPhoto(resource.getId());
        int flag = shopService.updateByPrimaryKeySelective(shop);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("flag", flag);
        return map;
    }

    //后台-店铺认证：获得所有店铺页面
    @RequestMapping("/shopManageAuthenticationPage")
    public ModelAndView shopManageAuthenticationPage() {
        ModelAndView modelAndView = new ModelAndView();
        if (SecurityUtils.getSubject().hasRole("ADMIN"))
            modelAndView.setViewName("manage/shopManage/authenticationShopList");
        else {
            SecurityUtils.getSubject().logout();
            modelAndView.setViewName("global/login");
        }
        return modelAndView;
    }

    //后台-店铺认证：获得所有店铺数据
    @RequestMapping("/getAuthenticationShopList")
    @ResponseBody
    public Map<String, Object> getAuthenticationShopList(@RequestParam Map<String, String> params) {
        int current = Integer.parseInt(params.get("current"));
        int rowCount = Integer.parseInt(params.get("rowCount"));
        params.put("startIndex", ((current - 1) * rowCount) + "");
        params.put("limit", "1");
        List<Map<String, Object>> rows = shopService.getAuthenticationShopList(params);
        params.put("limit", "0");
        List<Map<String, Object>> total = shopService.getAuthenticationShopList(params);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("total", total.get(0).get("total"));
        result.put("rows", rows);
        result.put("current", current);
        result.put("rowCount", rowCount);
        return result;
    }

    //后台-店铺认证：获得单个店铺页面
    @RequestMapping("/authenticationShop")
    public ModelAndView authenticationShop(@RequestParam Map<String, String> params) {
        ModelAndView modelAndView = new ModelAndView();
        if (SecurityUtils.getSubject().hasRole("ADMIN")) {
            Map<String, Object> shop = shopService.getShopForCheck(params);
            modelAndView.addObject("shop", shop);
            modelAndView.setViewName("manage/shopManage/authenticationShop");
        } else {
            SecurityUtils.getSubject().logout();
            modelAndView.setViewName("global/login");
        }
        return modelAndView;
    }

    //认证审核
    @RequestMapping("/check")
    @ResponseBody
    public Map<String, Object> check(@RequestParam Map<String, String> params) {
        Shop shop = new Shop();
        shop.setId(Integer.parseInt(params.get("id")));
        String checkResult = params.get("checkResult");
        if (checkResult.equals("1"))
            shop.setStatus(Shop.STATUS_CHECK_SUCCESS);
        else if (checkResult.equals("0"))
            shop.setStatus(Shop.STATUS_CHECK_FAIL);
        int flag = shopService.updateByPrimaryKeySelective(shop);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("flag", flag);
        return result;
    }

    @RequestMapping("/authenticationPage")
    public ModelAndView authenticationPage() {
        ModelAndView modelAndView = new ModelAndView();
        int userId = UserUtil.getCurrentUser().getId();
        Shop shop = shopService.getShopByUserId(userId);
        modelAndView.addObject("shop", shop);
        modelAndView.setViewName("home/shopManage/authentication");
        return modelAndView;
    }

    @RequestMapping("/shopMessagePage")
    public ModelAndView shopMessagePage(@RequestParam Map<String, String> params) {
        ModelAndView modelAndView = new ModelAndView();
        Map<String, Object> shop = shopService.getShopForCheck(params);
        modelAndView.addObject("shop", shop);
        modelAndView.setViewName("home/goodSearch/shopMessage");
        return modelAndView;
    }
}